<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="queryParams" @keyup.enter.native="loadData()">
      <el-form-item label="活动名称">
        <el-input v-model="queryParams.name" placeholder="活动名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selections.length === 0"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="名称"></el-table-column>
      <el-table-column prop="startTime" header-align="center" align="center" label="开始时间"></el-table-column>
      <el-table-column prop="endTime" header-align="center" align="center" label="结束时间"></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="scope.row.status"
            @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-paperclip" size="small" @click="relationProduct(scope.row.id)">关联商品
          </el-button>
          <el-button type="text" icon="el-icon-edit" size="small" @click="handleUpdate(scope.row.id)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />

    <seckill-form ref="dataForm" @ok="loadData"></seckill-form>

    <seckill-sku-relation ref="seckillskuRelation" :seckill-id="currentId"></seckill-sku-relation>

  </div>
</template>

<script>
import seckillForm from "./seckillForm.vue";
import seckillSkuRelation from "./seckillSkuRelation.vue";
import {deleteSeckill, seckillList, updateSeckill} from "@/api/coupon/seckill";

export default {
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      dataList: [],
      total: 0,
      loading: false,
      selections: [],
      addOrUpdateVisible: false,
      visible: false,
      currentId: 0
    };
  },
  components: {
    seckillForm,
    seckillSkuRelation
  },
  created() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.loading = true;
      seckillList().then(res => {
        if (res.success) {
          this.dataList = res.data.records;
          this.total = res.data.total;
        }
        this.loading = false;
      });
    },
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
      }
      this.loadData()
    },
    // 多选
    selectionChangeHandle(val) {
      this.selections = val;
    },

    handleAdd() {
      this.$refs.dataForm.add()
    },

    handleUpdate(id) {
      this.$refs.dataForm.edit(id);
    },

    relationProduct(id) {
      this.visible = true;
      this.currentId = id;
      this.$nextTick(() => {
        this.$refs.seckillskuRelation.loadData(this.currentId);
      });
    },
    handleStatusChange(row) {
      let data = {
        id: row.id,
        status: row.status
      }
      updateSeckill(data).then(res => {
        if (res.success) {
          this.$message.success("状态修改成功")
          this.loadData()
        }
      })
    },
    // 删除
    handleDelete(id) {
      let ids = id ? [id] : this.selections.map(item => {
        return item.id;
      });
      this.$confirm(
        `确定删除选中数据吗?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        deleteSeckill(ids).then(({data}) => {
          if (data && data.code === 0) {
            this.loadData();
            this.$message.success('删除成功')
          }
        });
      });
    }
  }
};
</script>
